package com.mq.dpca.msg;

import java.io.File;
import java.io.FileInputStream;

import com.ibm.mq.MQEnvironment;
import com.ibm.mq.MQException;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQPutMessageOptions;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;
import com.ibm.mq.constants.MQConstants;
import com.mq.dpca.msg.MQConfig;
import com.mq.dpca.util.Ping;
import com.mq.dpca.util.RenameUtil;
import com.mq.dpca.util.UuidUtil;

/**
 * 
 * 
 * 
 */
/**
 * @author Administrator
 * 
 */
public class MQMsgSender {

	final int BUFFER_LEN = 1024 * 1024 * 4; // 

	private MQQueueManager qmgr; // 

	private MQQueue outQueue; // 

	private String queueName = "Q1"; // 

	private String host = "127.0.0.1"; // 

	private int port = 1414; // 

	private String channel = null;; //

	public static String qmgrName = null; //

	private MQMessage outMsg; // 

	private MQPutMessageOptions pmo; // 

	private static String fileName; // 

	private int ccsid = 0;

	private static String file_dir = null;

	public static String fname = null;

	public static String key = null;
	
	private static String xml = null;

	public static String getFile_dir() {
		return file_dir;
	}

	public static void setFile_dir(String file_dir) {
		MQMsgSender.file_dir = file_dir;
	}

	// 104857000
	//
	public void runGoupSender() {
		try {
			init();
			sendGroupMessages();
			qmgr.commit();
			System.out.println("\n Messages successfully Send ");
		} catch (MQException mqe) {
			mqe.printStackTrace();
			try {
				System.out.println("\n Backing out Transaction ");
				qmgr.backout();
				System.exit(2);
			} catch (Exception e) {
				e.printStackTrace();
				System.exit(2);
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(2);
		}
	}

	public void initproperty(String key) {
		MQConfig config = new MQConfig().getNewDbConfigFromKey(key);
		if (config.getMQ_MANAGER() != null) {
			qmgrName = config.getMQ_MANAGER();
			queueName = config.getMQ_QUEUE_NAME();
			channel = config.getMQ_CHANNEL();
			host = config.getMQ_HOST_NAME();
			port = Integer.valueOf(config.getMQ_PROT());
			ccsid = Integer.valueOf(config.getMQ_CCSID());
			file_dir = config.getFILE_DIR();
		}
	}

	/**
	 * 
	 * @throws Exception
	 */
	private void init() throws Exception {
		/** */
		String hosts[] = host.split(";");
		String qmNames[] = qmgrName.split(";");
		String gt_ip = "";
		String gt_qmn = "";
		if (Ping.isAddressAvailable(hosts[0])) {
			gt_ip = hosts[0];
			gt_qmn = qmNames[0];
		} else {
			gt_ip = hosts[1];
			gt_qmn = qmNames[1];
		}
		MQEnvironment.hostname = gt_ip;

		MQEnvironment.channel = channel;
		MQEnvironment.port = port;
		MQEnvironment.CCSID = ccsid;
		// MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY,MQC.TRANSPORT_WEBSPHERE
		// MQ);
		qmgr = new MQQueueManager(gt_qmn);
		int opnOptn = MQConstants.MQOO_OUTPUT
				| MQConstants.MQOO_FAIL_IF_QUIESCING;
		outQueue = qmgr.accessQueue(queueName, opnOptn, null, null, null);
	}

	/**
	 * ���͵�������
	 * 
	 * @throws Exception
	 */
	public void sendGroupMessages() throws Exception {
		/* ���÷�����Ϣѡ�� */
		String uid = UuidUtil.getUUID();
		pmo = new MQPutMessageOptions();
		pmo.options = pmo.options + MQConstants.MQPMO_LOGICAL_ORDER;
		pmo.options = pmo.options + MQConstants.MQPMRF_GROUP_ID;
		outMsg = new MQMessage();
		/* ������Ϣ��ǣ���ʾ����Ϣ������ */
		outMsg.messageFlags = MQConstants.MQMF_MSG_IN_GROUP;
		int leght = 0;
		String buffer = null;
		while (true) {
			if(xml==null){
				break;
			}
			leght = xml.length();
			if (leght == 0) {
				
			}
			if (leght > BUFFER_LEN) {
				// ������Ƭ��С
				buffer = xml.substring(0, BUFFER_LEN);
				xml.replace(buffer, "");
			} else {
				// ����С����Ƭ��С�����ý�β��־��ţ���ʣ���ļ����ȫ�����ͣ�
				buffer = xml;
				outMsg.messageFlags = MQConstants.MQMF_LAST_MSG_IN_GROUP;
				xml = null;
			}
			// �����ļ����
			outMsg.setObjectProperty("serviceName", key);
			outMsg.setObjectProperty("requestId", uid);
			outMsg.setObjectProperty("requestSystem",
					RenameUtil.getSysForKey(key));
			outMsg.writeString(buffer);
			outQueue.put(outMsg, pmo);
			outMsg.clearMessage();
		}
	}

	/**
	 * ��������
	 * 
	 * @param args
	 */
	public static void main(String args[]) {
		String str = "<root><esbHeader><requestId>d5d001f47d46e1471ae42ebf1dded7a2</requestId><serviceName>syncCustmoerInfo</serviceName>"
				+ "<key>3DOxe6EfkvcAAAFH95SjNwAAAAAAAAAD</key></esbHeader><serviceNameReq><security><key>f7b17a6ffcd5561748c5bb98075aa432</key><date>2013-04-10 15:25:00</date></security>"
				+ "<customer><khmc>���Կͻ�ES2B</khmc><fzxsid>120483321</fzxsid><khczqh>010</khczqh><khczfj>1234</khczfj><khcz>567892012</khcz>"
				+ "</customer>"
				+ "<customer><khmc>���Կͻ�ES2B</khmc><fzxsid>120483321</fzxsid><khczqh>010</khczqh><khczfj>1234</khczfj><khcz>567892012</khcz>"
				+ "</customer>"
				+ "<customer><khmc>���Կͻ�ES2B</khmc><fzxsid>120483321</fzxsid><khczqh>010</khczqh><khczfj>1234</khczfj><khcz>567892012</khcz>"
				+ "</customer>" + "</serviceNameReq>" + "</root>";
		MQMsgSender mqMsgSender = new MQMsgSender();
		mqMsgSender.sendMsg("serviceName1", str);
		
	}

	public void sendMsg(String serviceName, String xml) {
		MQMsgSender mfs = new MQMsgSender();
		this.xml = xml;
		key = serviceName;
		mfs.initproperty(key);
		int i = 0;
		while (true) {
			i++;
			// �����ļ�
			mfs.runGoupSender();
			if (i == 1) {
				break;
			}
		}

	}
	
}